

class Plane extends egret.Bitmap {

	private textureName:string; // 资源名
	// public bmp:egret.Bitmap; // 图片

	private fireDelay:number; // 子弹速度
	private fireTimer:egret.Timer;	

	private offsetX:number;
	private offsetY:number;

	public constructor(textureName:string, fireDelay:number) {
		
		super(RES.getRes(textureName));

		this.textureName = textureName;
		this.fireDelay = fireDelay;

		//
		// this.bmp = new egret.Bitmap();
		// this.bmp.texture = RES.getRes(textureName);
		// this.addChild(this.bmp);

		//
		this.fireTimer = new egret.Timer(fireDelay);

		this.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onStartMove, this);
		this.addEventListener(egret.TouchEvent.TOUCH_END, this.onEndMove, this);
	}

	public fire() {
		this.fireTimer.removeEventListener(egret.TimerEvent.TIMER, this.onFireTimerDidTrick, this);
		this.fireTimer.addEventListener(egret.TimerEvent.TIMER, this.onFireTimerDidTrick, this);
		this.fireTimer.start();
	}

	public stopFire() {
		this.fireTimer.removeEventListener(egret.TimerEvent.TIMER, this.onFireTimerDidTrick, this);
		this.fireTimer.stop();
	}

	public onFireTimerDidTrick(et:egret.TimerEvent) {
		this.dispatchEventWith("CreateBullet");
	}

	public turnOnTouchEnabled(turn:boolean) {
		this.touchEnabled = turn;
	}

	//
	private onStartMove(et:egret.TouchEvent) {
				console.log(et.stageX, et.stageY);

		this.offsetX = et.stageX - this.x;
		this.offsetY = et.stageY - this.y;
		this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.onTouchMove, this);
	}

	private onEndMove(et:egret.TouchEvent) {
				console.log(et.stageX, et.stageY);

		this.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE, this.onTouchMove, this);
	}

	private onTouchMove(et:egret.TouchEvent) {
		this.x = et.stageX - this.offsetX;
		this.y = et.stageY - this.offsetY; 
		console.log(et.stageX, et.stageY);
		
	}







	private static poolDict:Object = {};

	public static produce(textureName:string, fireDelay:number) : Plane {
		var plane:Plane;

		if (Plane.poolDict[textureName] == null) {
			Plane.poolDict[textureName] = [];
		}
		var arr:Plane[] = Plane.poolDict[textureName];

		if (arr.length > 0) {
			plane = arr.pop();
		} else {
			plane = new Plane(textureName, fireDelay);
		}
		return plane;
	}

	public static reclaim(plane:Plane) {
		if (Plane.poolDict[plane.textureName] == null) {
			Plane.poolDict[plane.textureName] = [];
		}
		var arr:Plane[] = Plane.poolDict[plane.textureName];
		if (arr.indexOf(plane) == -1) {
			arr.push(plane);
		}
	}


}